package org.apache.struts.ehr.medical_records.action;

import java.util.Map;

import com.earogya.communicate.Communicator;
import com.earogya.logic.medical_records.CreateADTA02;
import com.earogya.logic.medical_records.CreateQRYA19;
import com.earogya.logic.medical_records.ParseReply;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.struts.ehr.medical_records.model.HL7Message;
import org.apache.struts.ehr.medical_records.model.ReplyMessage;
import org.apache.struts.ehr.medical_records.model.TransferToAnotherWard;

public class TransferPatientAction extends ActionSupport{

	private TransferToAnotherWard transferToAnotherWard;
	private HL7Message message;
	private ReplyMessage replyMessage;

	public HL7Message getMessage() {
		return message;
	}
	
	public ReplyMessage getReplyMessage() {
		return replyMessage;
	}

	public TransferToAnotherWard getTransferToAnotherWard() {
		return transferToAnotherWard;
	}

	public void setTransferToAnotherWard(TransferToAnotherWard transferToAnotherWard) {
		this.transferToAnotherWard = transferToAnotherWard;
	}

	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		replyMessage=new ReplyMessage();
		replyMessage.setMessage("empty");
		CreateADTA02 createMessage=new CreateADTA02(transferToAnotherWard);
		String msg=createMessage.createMessage("CSTH", "ward");
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			if(!reply.equals("No Reply")){
				ParseReply parse=new ParseReply();
				replyMessage=(ReplyMessage)parse.parseMessage(reply);
			}
		}
		else{
			message.setMessage("empty");
		}
		return SUCCESS;
	}
	
	public void validate(){
		if(transferToAnotherWard.getPatientId().length()==0){
			addFieldError("transferToAnotherWard.patientId", "This field is required");
		}
		if(!this.isInteger(transferToAnotherWard.getPatientId())){
			addFieldError("transferToAnotherWard.patientId", "Integer required");
		}
		if(transferToAnotherWard.getHospital().length()==0){
			addFieldError("transferToAnotherWard.hospital", "This field is required");
		}
		if(transferToAnotherWard.getTransferTime()==null){
			addFieldError("transferToAnotherWard.transferTime", "This field is required");
		}
		if(transferToAnotherWard.getFromBedNo().length()==0){
			addFieldError("transferToAnotherWard.fromBedNo", "This field is required");
		}
		if(!this.isInteger(transferToAnotherWard.getFromBedNo())){
			addFieldError("transferToAnotherWard.fromBedNo", "Incorrect bed number format");
		}
		if(transferToAnotherWard.getFromWardNo().length()==0){
			addFieldError("transferToAnotherWard.toWardNo", "This field is required");
		}
		if(!this.isInteger(transferToAnotherWard.getFromWardNo())){
			addFieldError("transferToAnotherWard.toWardNo", "Incorrect ward number format");
		}
		if(transferToAnotherWard.getToBedNo().length()==0){
			addFieldError("transferToAnotherWard.toBedNo", "This field is required");
		}
		if(!this.isInteger(transferToAnotherWard.getToBedNo())){
			addFieldError("transferToAnotherWard.toBedNo", "Incorrect bed number format");
		}
		if(transferToAnotherWard.getToWardNo().length()==0){
			addFieldError("transferToAnotherWard.toWardNo", "This field is required");
		}
		if(!this.isInteger(transferToAnotherWard.getToWardNo())){
			addFieldError("transferToAnotherWard.toWardNo", "Incorrect ward number format");
		}
		if(transferToAnotherWard.getTransferringOfficerId().length()==0){
			addFieldError("transferToAnotherWard.transferringOfficerId", "This field is required");
		}
		if(transferToAnotherWard.getReason().length()==0){
			addFieldError("transferToAnotherWard.reason", "This field is required");
		}

	}
	
	private Boolean isInteger(String value){
		Boolean isInteger=false;
		try{
			int intValue=Integer.parseInt(value);
			isInteger=true;
		}catch(NumberFormatException e){
			isInteger=false;
		}
		return isInteger;
	}
}
